﻿using System;
using SantaSLittleIrcHelper.Irc;
using SantaSLittleIrcHelper;
using System.Drawing;

namespace SantaSLittleIrcHelper
{

    public enum StatusFlag { Disconnected = 0, Connecting = 1, Connected = 2, Disconnecting = 3 }

    class ConnectionStatus
    {

        private delegate void SetLabel(Color color, string text);
        private StatusFlag _currentStatus = 0;

        public StatusFlag CurrentStatus { get { return this._currentStatus; } private set { this._currentStatus = value; } }

        public ConnectionStatus()
        {
            Program.instance.IRC.OnConnectionStatusChanged += new ConnectionStatusChangedEventHandler(this.statusChanged);
        }

        public void statusChanged(object sender, ConnectionStatusChangedEventArgs e)
        {
            this._currentStatus = e.Status;
            switch (this._currentStatus)
            {
                case StatusFlag.Disconnected:
                    {
                        Program.instance.consoleWrite(Languages.Instance.get("successfullyDisconnected"));
                        break;
                    }
                case StatusFlag.Connected:
                    {
                        Program.instance.consoleWrite(Languages.Instance.get("successfullyConnected"));
                        if (Program.instance.currentProcess != null) Program.instance.currentProcess.start();
                        break;
                    }
                case StatusFlag.Connecting:
                    {
                        if (Program.instance.settings.verboseMode)
                        {
                            Program.instance.consoleWrite(String.Format(Languages.Instance.get("connectingVerbose"), Program.instance.nickname));
                        }
                        else
                        {
                            Program.instance.consoleWrite(Languages.Instance.get("connecting"));
                        }
                        break;
                    }
            }
            this.update();
        }

        public void update()
        {
            Color col;
            switch (this._currentStatus)
            {
                case StatusFlag.Disconnected:
                    {
                        col = Color.Red;
                        break;
                    }
                case StatusFlag.Connected:
                    {
                        col = Color.Green;
                        break;
                    }
                default:
                    {
                        col = Color.Orange;
                        break;
                    }
            }
            String text = Languages.Instance.get("status" + _currentStatus.ToString());
            SetLabel d = new SetLabel(Program.instance.GUI.invokeChangeStatus);
            Program.instance.GUI.Invoke(d, new object[] { col, text });

        }

    }
}
